﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using DDDProject.Aspects;
using DDDProjectSample.Domain.Errors;
using DDDProjectSample.Application.Controllers;

namespace DDDProjectSample.WebUI
{
    public partial class NewTopic : System.Web.UI.Page
    {
        // -------------------------------------------------------------------------
        // Here we declare a private field that will holds a reference to 
        // the controller instance. We decorate it with the [InjectComponent] 
        // aspect, in order to auto-initialize the field at runtime.
        // Note that your page can interact with more than one controller, if
        // needed. Just declare some other fields like the one below.
        // -------------------------------------------------------------------------
        [InjectComponent]
        private ForumsController _forumsController;

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            // ------------------------------------------------------------------------
            // When the user click on the « ButtonSave » button, he asks the system 
            // to create a new topic.
            // So here we just immediatly call the controller's « CreateTopic »
            // method. The CreateTopic method needs 4 arguments : 
            // the title of the topic, the Id of the forum, the name of the user that 
            // wants to create the topic, and the text of the first message that will 
            // be posted for the topic. These parameters are collected by the UI layer,
            // and passed in the method call.
            // ------------------------------------------------------------------------

            try
            {
                // Creates the new topic
                _forumsController.CreateTopic(
                    TextBoxTitle.Text,
                    Int64.Parse(this.Request.QueryString["ForumId"]),
                    User.Identity.Name,
                    TextBoxMessage.Text);

                // Redirects to the forum's page
                RedirectToForumPage();
            }
            catch (DomainError error)
            {
                // ----------------------------------------------------------------
                // Use DomainError to manage business rules. Domain objects can
                // throw some domain exceptions, that inform the UI layer that a
                // business rule was not satisfied, so you must treat them here.
                // ----------------------------------------------------------------

                Master.ErrorMessage = error.Message;

                return;
            }
        }

        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            // -----------------------------------------------------------
            // Some user actions does not need to ask a domain controller.
            // Here an example of a cancel action that only redirect the
            // user to another page.
            // -----------------------------------------------------------

            // Redirects to the forum's page
            RedirectToForumPage();
        }

        private void RedirectToForumPage()
        {
            Response.Redirect(String.Concat(
                "~/Forum.aspx?Id=",
                Request.QueryString["ForumId"]));
        }
    }
}
